package freespiritsocial.io.user.ui.fragment.search

import android.os.Bundle
import androidx.lifecycle.ViewModelStoreOwner
import com.scwang.smart.refresh.layout.util.SmartUtil.dp2px
import freespiritsocial.io.user.R
import freespiritsocial.io.user.base.BaseFragment
import freespiritsocial.io.user.databinding.FrgSuggestionsBinding
import freespiritsocial.io.user.decoration.SpaceItemDecoration
import freespiritsocial.io.user.ext.init
import freespiritsocial.io.user.ui.adapter.search.SuggestionsAdapter
import freespiritsocial.io.user.viewmodel.SearchViewModel
import me.hgj.jetpackmvvm.ext.util.loge

/**
 * @description 搜索建议
 */
class SuggestionsFrg: BaseFragment<SearchViewModel,FrgSuggestionsBinding>() {
    companion object {
        fun crate(viewModelStore: ViewModelStoreOwner) =
            SuggestionsFrg().apply {
                viewModelOwner = viewModelStore
            }

    }


    val adapter by lazy(LazyThreadSafetyMode.NONE) {
        SuggestionsAdapter(mViewModel).apply {
            loadMoreModule.setOnLoadMoreListener {
                data.lastOrNull()?.placeId?.loge("csw")
                mViewModel.getSearchList(SearchViewModel.suggestios,data.lastOrNull()?.placeId)
            }
        }
    }

    override fun layoutId(): Int = R.layout.frg_suggestions

    override fun initView(savedInstanceState: Bundle?) = with(mDatabind) {
        recyclerView.init(adapter,emptyText = getString(R.string.search_empty_message),emptyImage = R.mipmap.empty_search).addItemDecoration(
            SpaceItemDecoration(0,dp2px(20f))
        )
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.searchIng.observe(this) {
            if (it) {
                adapter.data.clear()
                mViewModel.getSearchList(SearchViewModel.suggestios)
            }
        }
        mViewModel.suggestiosResult.observe(this) {
            adapter.addData(it)
            adapter.loadMoreModule.loadMoreComplete()
            adapter.loadMoreModule.isEnableLoadMore = it.isNotEmpty()
        }
    }

    inner class Click
}